package com.example.weather.Other;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.os.Build;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
/**
 * @author Y7993
 */ //广播 网络的文件
public class NetworkStateBroadcast extends BroadcastReceiver {

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network network=cm.getActiveNetwork();
        if(network==null){
            Toast.makeText(context,"当前网络无连接",Toast.LENGTH_SHORT).show();
            System.out.println("当前网络无连接");
        }
        else{
            Toast.makeText(context,"当前网络恢复连接",Toast.LENGTH_SHORT).show();
            System.out.println("当前网络恢复连接");
        }
    }
}
